package com.snc.controller.app;


import com.snc.controller.index.BaseController;
import com.snc.entity.PkFormMap;
import com.snc.entity.SchoolFormMap;
import com.snc.entity.StuTeacherFormMap;
import com.snc.entity.PkFormMap;
import com.snc.plugin.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * APP-活动
 */
@Controller
@RequestMapping("/app/pk")
public class PkController extends BaseController {

    /**
     * 获取活动的列表，
     * 请求参数：
     *
     * @return 数据
     */
    @ResponseBody
    @RequestMapping(value = "/list")
    public Result list() {
        PkFormMap map = super.toFormMap(PkFormMap.class);
        String str = " 1=1 ";
        if(map.getStr("time") != null){
            str+= " and time = '"+map.getStr("time")+"'";
        }
        if(map.getStr("type") != null){
            str+= " and type = '"+map.getStr("type")+"'";
        }
        if(map.getStr("teacherId") != null){
            str+= " and teacherId = "+map.getStr("teacherId");
        }
        if(map.getStr("school") != null){
            str+= " and school = "+map.getStr("school");
        }
        map.put("where",str);

        List<PkFormMap> list = map.findByWhere();
        for (PkFormMap pk : list) {
            pk.put("school",new SchoolFormMap().findById(pk.getLong("school")+""));
        }
        return Result.Success(list);
    }

    @ResponseBody
    @RequestMapping("/add")
    public Result add() throws Exception {
        PkFormMap map = super.toFormMap(PkFormMap.class);
        map.save();
        return Result.Success(null);
    }

    @ResponseBody
    @RequestMapping("/del")
    public Result del(String id) throws Exception {
        PkFormMap map = super.toFormMap(PkFormMap.class);
        map.deleteById(id);
        return Result.Success(null);
    }

}